﻿using System;
using System.Collections.Generic;
using System.Text;
using UpdateControls.XAML;
using UpdateControls;

namespace SQLBuilderWPF
{
    public class MapinfoSQLExpression : SQLExpression
    {
        private Independent _indFromExpression = new Independent();
        private Independent _indSelectExpression = new Independent();
        private Independent _indWhereExpression = new Independent();

        private SelectExpression select;
        public SelectExpression Select
        {
            get { _indSelectExpression.OnGet(); return select; }
            set { _indSelectExpression.OnSet(); select = value; }
        }

        private FromExpression from;
        public FromExpression From
        {
            get { _indFromExpression.OnGet(); return from; }
            set { _indFromExpression.OnSet(); from = value; }
        }

        private WhereExpression where;
        public WhereExpression Where
        {
            get { _indWhereExpression.OnGet(); return where; }
            set { _indWhereExpression.OnSet(); where = value; }
        }

        public override string ToExpression()
        {
            StringBuilder sb = new StringBuilder();
            if (this.Select == null)
            {
                this.Select = new SelectExpression();
            }
            sb.Append(this.Select.ToExpression());
            sb.Append(" ");
            sb.Append(this.From.ToExpression());
            sb.Append(" ");
            sb.Append(this.Where.ToExpression());
            return sb.ToString();
        }

        public void ParseString(string p)
        {
            int selectindex = p.IndexOf("select");
            int endselectindex = p.IndexOf("select",0) + 6;
            int fromindex = p.IndexOf("from");
            int fromendindex = fromindex + 4;
            int whereindex = p.IndexOf("where");
            int whereendindex = whereindex + 5;

            if (selectindex >= 0)
            {
                string projections = p.Substring(endselectindex, fromindex - endselectindex).Trim();
                this.Select.Projections = projections;
            }
            else
            {
                this.Select.Projections = "";
            }

            if (fromindex > 0)
            {
                int length = whereindex > 0 ? whereindex : p.Length;

                string tables = p.Substring(fromendindex, length - fromendindex).Trim();
                this.From.TableList = tables;
            }
            else
            {
                this.From.TableList = "";
            }

            if (whereindex > 0)
            {
                string conditions = p.Substring(whereendindex, p.Length - whereendindex).Trim();
                this.Where.Conditions = conditions;
            }
            else
            {
                this.Where.Conditions = "";
            }
        }
    }

    public abstract class SQLExpression
    {
        public abstract string ToExpression();
    }

    public class SelectExpression : SQLExpression
    {
        private string projections = "*";
        private Independent _indProjections = new Independent();

        public string Projections
        {
            get { _indProjections.OnGet(); return projections; }
            set { _indProjections.OnSet(); projections = value; }
        }

        public override string ToExpression()
        {
            if (string.IsNullOrEmpty(this.Projections))
            {
                this.Projections = "*";
            }
            return "select " + this.Projections;
        
        }
    }

    public class FromExpression : SQLExpression
    {
        private string tableName;
        private Independent _indTableName = new Independent();

        public string TableList
        {
            get { _indTableName.OnGet(); return tableName; }
            set { _indTableName.OnSet(); tableName = value; }
        }

        public override string ToExpression()
        {
            return "from " + this.TableList;
        }
    }
}
